package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsOrderGoodsMapper;
import com.shop.mapper.DtsOrderMapper;
import com.shop.mapper.DtsUserMapper;
import com.shop.pojo.*;
import com.shop.service.DtsOrderService;
import com.shop.service.DtsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsOrderServiceImpl
 * @Author xiao宋
 * @Date 21:38  2022/10/13
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsOrderServiceImpl implements DtsOrderService {
    @Autowired
    DtsOrderMapper orderMapper;
    @Autowired
    DtsOrderGoodsMapper goodsMapper;
    @Autowired
    DtsUserMapper userMapper;

    @Override
    public long getTotal() {
        LambdaQueryWrapper<DtsOrder> w = new LambdaQueryWrapper<>();
        w.eq(DtsOrder::getDeleted, false);

        Integer total = orderMapper.selectCount(w);
        return total;
    }

    @Override
    public Map<String, Object> findOrderPage(Integer page, Integer limit, String sort, String order, Integer userId, Integer orderSn, Integer[] orderStatusArray) {
        IPage<DtsOrder> iPage = new Page<>(page, limit);
        QueryWrapper w = new QueryWrapper<>();
        if (userId != null) {
            w.eq("user_id", userId);
        }
        if (orderSn != null) {
            w.eq("order_sn", orderSn);
        }

        if (orderStatusArray != null && orderStatusArray.length > 0) {
            w.in("order_status", orderStatusArray);
        }

//        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }

        orderMapper.selectPage(iPage, w);
//      封装数据
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());

        return data;
    }

    @Override
    public Map<String, Object> getOrderDetail(Integer id) {
        QueryWrapper<DtsOrder> wrapper = new QueryWrapper<>();
        if (id != null) {
            wrapper.eq("id", id);

        }
        DtsOrder order = orderMapper.selectById(wrapper);
        if (order == null) {
            throw new RuntimeException("没查到此条数据");
        }
        QueryWrapper<DtsOrderGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("order_id", order.getId());
        DtsOrderGoods orderGoods = goodsMapper.selectById(wrapper1);
        QueryWrapper<DtsUser> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("id",order.getId());
        DtsUser user = userMapper.selectById(wrapper2);
        user.setPassword("");
        Map<String, Object> data = new HashMap<>();

        data.put("orderGoods", orderGoods);
        data.put("user", user);
        data.put("order", order);
        return data;
    }
}
